<?php
namespace BeautyParlorAdmin\Controller;

use Common\Util\UploadHandler;

class BeautyParlorController extends BaseController {
    public function index(){
    	$Model = D('BeautyParlor');
    	 
    	$list = $Model->select();
    	 
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function add() {
    	if (!IS_POST) {
    		$this->assign('number', md5(microtime()));
    		$this->display();
    	} else {
    		$BeautyParlorModel = D('BeautyParlor');
    		if ($BeautyParlorModel->addBeautyParlor(I('post.'))) {
    			$this->success('添加成功');
    		} else {
    			$this->error($BeautyParlorModel->getError());
    		}
    	}
    }
    
    /**
     * @todo 美容院添加美容师
     */
    public function addEmployee() {
    	if (!IS_POST) {
    		$beautyId = I('get.id');
    		$this->assign('beauty_id', $beautyId);
    		
    		$beautyInfo = D('BeautyParlor')->getBeautyInfo($beautyId);
    		$beauticians = D('beautician')->where(array('province' => $beautyInfo ['province'], 'city' => $beautyInfo ['city']))->select();
    		$this->assign('beauticians', $beauticians);
    		
    		$M = M('beauty_parlor_employe');
    		
    		$oldEmp = $M->where(array('bp_id' => $beautyId))->select();
    		$oldEmpIds = array();
    		if ($oldEmp) {
    			$oldEmpIds = array_map(function ($var) {return $var ['beautician_id'];}, $oldEmp);
    			$this->assign('employees', $oldEmpIds);
    		}
    		
    		$this->display();
    	} else {
    		$beautyId = I('post.id');
    		$beauticians = I ('post.beautician_id');
    		
    		$M = M('beauty_parlor_employe');
    		$oldEmp = $M->where(array('bp_id' => $beautyId))->select();
    		$oldEmpIds = array();
    		if ($oldEmp) {
    			$oldEmpIds = array_map(function ($var) {return $var ['beautician_id'];});
    		}
    		
    		$deleteEmp = array_diff($oldEmpIds, $beauticians);
    		$addEmp = array_diff($beauticians, $oldEmpIds);
    		
    		$M->startTrans();
    		if ($deleteEmp) {
    			foreach($deleteEmp as $dKey => $dValue) {
    				$deleteResult = $M->where(array('bp_id' => $beautyId, 'beautician_id' => $dValue))->save(array('status' => 0));
    				if (!$deleteResult) {
    					$M->rollback();
    					$this->error('删除美容师关系失败');
    					exit;
    				}
    			}
    		}
    		
    		if  ( $addEmp) {
    			foreach($addEmp as $aKey => $aValue) {
    				$data = array('bp_id' => $beautyId, 'beautician_id' => $aValue, 'created' => time());
    				$addResutl = $M->add($data);
    				if (!$addResutl) {
    					$M->rollback();
    					$this->error('建立美容师关系失败'.$M->getError());
    					exit;
    				}
    			}
    		}
    		
    		$M->commit();
    		$this->success('添加美容师成功');
    	}
    }
    
    /**
     * @todo 地址转百度经纬度
     */
    public function getCoordinates(){
    	$arrData=CheckCoordinate();
    	$this->ajaxReturn($arrData);
    }
    
    public function edit() {
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$Model = D('BeautyParlor');
    		$PhotoModel = D('Photo');
    		
    		$item = $Model->getBeautyInfo($id);
    		
    		$PhotoModel->type = 'cover';
    		$covers = $PhotoModel->getTargetPhotos($id);
    		
    		if ($photos) {
    			$this->assign('covers', $covers);
    			$this->assign('number', $photos [0] ['upload_number']);
    		} else {
    			$this->assign('number', md5(microtime()));
    		}
    		
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$BeautyParlorModel = D('BeautyParlor');
    		if ($BeautyParlorModel->updateInfo(I('post.'))) {
    			$this->success('修改成功', U('/Admin/BeautyParlor/index'));
    		} else {
    			$this->error($BeautyParlorModel->getError());
    		}
    	}
    }
    
    public function delete() {
    	
    }
    
    /**
     * @todo 美容院资质设置
     */
    public function setZizhi() {
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$this->assign('id', $id);
    		$setting = M('beauty_parlor_zizhi')->where(array('bp_id' => $id))->find();
    		$this->assign('setting', $setting);
    		$this->display();
    	} else {
    		$id = I ('get.bp_id');
    		$data = I ('post.');
    		
    		$setting = M('beauty_parlor_zizhi')->where(array('bp_id' => $id))->find();
    		if ($setting) {
    			unset($data ['id']);
    			$result = M('beauty_parlor_zizhi')->where(array('bp_id' => $id))->save($data);
    		} else {
    			$result = M('beauty_parlor_zizhi')->add($data);
    		}
    		
    		if ($result) {
    			$this->success('美容院资质设置成功！', U('/Admin/BeautyParlor/index'));
    		} else {
    			$this->success('美容院资质设置失败！');
    		}
    	}
    }
    
    /**
     * @todo 上传美容院图标
     */
    public function uploadBeautyParlorBanner() {
    	$options = C('BEAUTY_PARLOR_BANNER_UPLODE_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Beauty/Banner/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	 
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    /**
     * @todo 上传美容院封面轮播图
     */
    public function uploadBeautyParlorCover() {
    	$num = I('post.num');
    	$options = C('BEAUTY_PARLOR_COVER_UPLODE_CONFIG');
    	$options ['upload_dir'] .= $num.'/';
    	$options ['upload_url'] = get_full_url().'/Uploads/Beauty/Cover/'.$num.'/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name]);
    }
}